<template>
	<view class="register">
		<u-toast ref="uToast"></u-toast>
		<view>
			<navbar title='注册' :is-child="true" />
		</view>
		<view class="content">
			<!-- 头部logo -->
			<view class="header" style="background-color: #d6d6d6; width: 300rpx; height: 300rpx;">
				<image src="https://img2.imgtp.com/2024/04/01/Cd8a3lY3.png" style="width: 300rpx; height: 300rpx;"></image>
			</view>
			<!-- 主体 -->
			<view class="main">
				<wInput
					v-model="registerInfo.username"
					type="text"
					maxlength="20"
					placeholder="用户名"
				></wInput>
				<wInput
					v-model="registerInfo.email"
					type="text"
					maxlength="20"
					placeholder="邮箱"
				></wInput>
				<wInput
					v-model="registerInfo.password"
					type="password"
					maxlength="20"
					placeholder="登录密码"
					isShowPass
				></wInput>
			</view>
				
			<wButton 
				class="wbutton"
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
			
			<!-- 底部信息 -->
			<view class="footer">
				<text 
					@tap="isShowAgree" 
					class="cuIcon"
					:class="showAgree?'cuIcon-radiobox':'cuIcon-round'"
				> 同意</text>
				<!-- 协议地址 -->
				<navigator url="/pages/me/user-protocol/user-protocol" open-type="navigate">《用户须知》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '@/components/watch-login/watch-input.vue' //input
	import wButton from '@/components/watch-login/watch-button.vue' //button
	import { register } from '@/apis/user.js'
	
	export default {
		data() {
			return {
				phoneData:'', // 用户/电话
				passData:'', //密码
				verCode:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
				registerInfo: {
					username: '',
					password: '',
					email: ''
				}
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
		},
		methods: {
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
		    startReg() {
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《用户须知》'
				    });
				    return false;
				}
				if (this.registerInfo.username.length  === 0) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return false;
				}
				if (this.registerInfo.password.length  === 0) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '密码不能为空'
				    });
				    return false;
				}
				if (this.registerInfo.email.length  === 0) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '邮箱不能为空'
				    });
				    return false;
				}
				
				_this.isRotate=true;
				register(_this.registerInfo).then(res => {
					uni.showToast({
						icon: 'success',
						position: 'bottom',
						title: '注册成功'
					});
					setTimeout(function(){
						uni.navigateTo({
							url: '/pages/me/login/login'
						})
					}, 1000);
				}).catch(e => {
					if(e.code != undefined) {
						let message = e.message;
						uni.showToast({
							icon: 'error',
							position: 'bottom',
							title: message || '注册失败'
						});
					}
				}).finally( r => {
					_this.isRotate=false;
				})
		    }
		}
	}
</script>

<style>
	@import url("@/components/watch-login/css/icon.css");
	@import url("@/common/search.css");
</style>